package com.summer.controller;


import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.entity.TbSign;
import com.summer.entity.TbSignIn;
import com.summer.entity.TbUserinfo;
import com.summer.mapper.TbSignInMapper;
import com.summer.mapper.TbUserinfoMapper;
import com.summer.utils.JwtToken;
import com.summer.utils.MyUtil;
import com.summer.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author summer502
 * @since 2021-03-31
 */
@CrossOrigin
@RestController
@RequestMapping("/tb-sign-in")
@Transactional
@Api(value = "TbSignInController", description = "签到api")

public class TbSignInController {

    /**
     *  private Integer signInId;
        private Integer acitvityId;
        private Integer userId;
        private LocalDateTime signInTime;
        private LocalDateTime signOutTime;
        private Integer isApply;
        private Date create_time;
        private Date update_time;
        private Integer isDelete;
     */
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired (required = false)
    TbSignInMapper tbSignInMapper;

    @ApiOperation("超级管理员添加签到")
    @PostMapping("/teacher/addTbSignIn")
    @ResponseBody
    public ResponseEntity addTbSignIn(@RequestParam(required = false) int activityId,
                                      @RequestParam(required = false) int userId,
                                      @RequestParam(required = false) String signInTime,
                                    @RequestParam(required = false) String signOutTime,
                                      @RequestParam(required = false) int isApply,
                                    @RequestHeader String Authorization
    ) throws IOException, TokenExpiredException {
        ResponseEntity responseEntity;
        String token = Authorization;
        //parentRoleId
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo tbUserinfo = JwtToken.unsign(token, TbUserinfo.class);
        //用户角色，分微信用户和老师用户，1代表微信用户，2代表老师用户,默认为微信用户
        if (tbUserinfo.getUserId()!=1&&tbUserinfo.getUserId()>0){
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg","权限错误");
            responseEntity.putDataValue("timestamp",myUtil.getTime());
            return responseEntity;
        }
        //根据user_jobNumber查询数据库是否有该用户存在，不存在则注册，存在则注册失败
        QueryWrapper<TbSignIn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId);
        TbSignIn tbSignIn = tbSignInMapper.selectOne(queryWrapper);
        if (tbSignIn == null){
            responseEntity = ResponseEntity.ok();
            TbSignIn tbSignIn1 = new TbSignIn();
            tbSignIn1.setIsApply(isApply);
            tbSignIn1.setSignInTime(signInTime);
            tbSignIn1.setSignOutTime(signOutTime);
            tbSignIn1.setUserId(userId);
            tbSignInMapper.insert(tbSignIn1);
            responseEntity.putDataValue("signInId",tbSignIn1.getSignInId());
            responseEntity.putDataValue("msg","活动签到添加成功");
        }else {
            responseEntity = ResponseEntity.badRequest();
            responseEntity.putDataValue("msg","活动签到添加失败");
        }
        return responseEntity;
    }

    /**
     * 活动签到列表
     * @return
     * @throws IOException
     */
    @ApiOperation("活动签到列表")
    @PostMapping("/web/TbSignInList")
    @ResponseBody
    public ResponseEntity TbSignInList() throws IOException {
        ResponseEntity responseData;
        QueryWrapper<TbSignIn> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<TbSignIn> list = tbSignInMapper.selectList(wrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("list",list);
        responseData.putDataValue("msg","成功");
        return responseData;
    }

    /**
     * 根据id删除活动报名
     * @param signInId
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id删除活动签到")
    @PutMapping("/web/deleteTbSignIn")
    @ResponseBody
    public ResponseEntity deleteTbSignIn(@RequestParam(required = true) Integer signInId,@RequestHeader String Authorization)throws IOException{
        ResponseEntity responseEntity;
        String token=Authorization;
        if(flag=myUtil.judgeToken(token)){
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users=JwtToken.unsign(token,TbUserinfo.class);
        if(users==null){
            responseEntity=ResponseEntity.notFound();
            responseEntity.putDataValue("msg","用户不存在");
            return responseEntity;
        }else if(users.getRoleId()==1) {
            TbSignIn tbSignIn = tbSignInMapper.selectById(signInId);
            if(tbSignIn!=null){
                int is_delete = 1;
                tbSignIn.setIsDelete(is_delete);
                tbSignInMapper.updateById(tbSignIn);
                responseEntity=ResponseEntity.ok();
                responseEntity.putDataValue("msg","删除成功");
                return responseEntity;
            }
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 根据applyId查询活动签到
     * @param signInId
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据applyId查询活动签到")
    @PutMapping("/web/selectByTbSignInId")
    @ResponseBody
    public ResponseEntity selectByTbSignInId(@RequestParam(required = true) Integer signInId, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getRoleId()==1) {
            responseEntity=ResponseEntity.ok();
            TbSignIn tbSignIn = tbSignInMapper.selectById(signInId);
            responseEntity.putDataValue("tbApply",tbSignIn);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 超级管理员修改活动签到
     * @param signInId
     * //@param activityId
     * @param userId
     * @param signInTime
     * @param signOutTime
     * @param isApply
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("超级管理员修改活动签到")
    @PutMapping("/web/updateTbSignIn")
    @ResponseBody
    public ResponseEntity updateTbSignIn(@RequestParam(required = true) int signInId,
                                         //@RequestParam(required = false) int activityId,
                                         @RequestParam(required = false) int userId,
                                         @RequestParam(required = false) String signInTime,
                                         @RequestParam(required = false) String signOutTime,
                                         @RequestParam(required = false) int isApply,
                                       @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getRoleId()!=1) {
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg", "没有权限");
            return responseEntity;

        } else {
            responseEntity = ResponseEntity.ok();
            TbSignIn tbSignIn1 = tbSignInMapper.selectById(signInId);
            tbSignIn1.setIsApply(isApply);
            tbSignIn1.setSignInTime(signInTime);
            tbSignIn1.setSignOutTime(signOutTime);
            tbSignIn1.setUserId(userId);
            tbSignInMapper.updateById(tbSignIn1);
            responseEntity.putDataValue("msg", "活动签到修改成功");
            return responseEntity;
        }
    }


}
